package com.paic.qhcs.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.apache.commons.lang.math.RandomUtils;

import com.paic.qhcs.util.entry.Header;
import com.paic.qhcs.util.entry.MessageEntry;
import com.paic.qhcs.util.entry.SecurityInfo;

/**
 * @author XSunny
 *
 */
public class MessageUtil
{

    /**
     * @param header
     * @return
     */
    public static String getMHeader_DMZ(Header header)
    {
    	SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	String aDate;
    	String tDate;
    	if(header.getAuthDate() == null){
    		aDate = new String(fmt.format(new Date()));
    	}else{
    		aDate = new String(fmt.format(header.getAuthDate()));
    	}
    	if(header.getTransDate() == null){
    		tDate = new String(fmt.format(new Date()));
    	}else{
    		tDate = new String(fmt.format(header.getTransDate()));
    	}
    	
        StringBuffer sb = new StringBuffer(
                "{\"orgCode\":\""+header.getOrgCode()+"\","
                + "\"chnlId\":\""+ header.getChnlId()+"\","
                + "\"transNo\":\""+header.getTransNo()+"\","
        		+ "\"transDate\":\""+tDate+"\","
				+ "\"authCode\":\""+header.getAuthCode()+"\","
				+ "\"authDate\":\""+aDate+"\"}");
        return sb.toString();
    }
    
    /**
     * @param batchNo
     * @param messages
     * @return
     */
    public static String getBusiData(String batchNo, List<MessageEntry> messages){
    	StringBuffer sb = new StringBuffer(
                "{\"batchNo\":\""+batchNo+"\",\"records\":[");
    	int i = 0;
    	for(MessageEntry message : messages){
    		sb.append("{\"reasonNo\":\""+message.getReasonNo()+"\",");
    		sb.append("\"idNo\":\""+message.getIdNo()+"\",");
    		sb.append("\"idType\":\""+message.getIdType()+"\",");
    		sb.append("\"mobileNo\":\""+message.getMobileNo()+"\",");
    		sb.append("\"name\":\""+message.getName()+"\",");
    		sb.append("\"cardNo\":\""+message.getCardNo()+"\",");
    		
    		if(message.getAmazonNo() != null){sb.append("\"amazonNo\":\""+message.getAmazonNo()+"\",");}
    		if(message.getEmail() != null){sb.append("\"email\":\""+message.getEmail()+"\",");}
    		if(message.getWeiboNo() != null){sb.append("\"weiboNo\":\""+message.getWeiboNo()+"\",");}
    		if(message.getWeixinNo() != null){sb.append("\"weixinNo\":\""+message.getWeixinNo()+"\",");}
    		if(message.getQqNo() != null){sb.append("\"qqNo\":\""+message.getQqNo()+"\",");}
    		if(message.getJdNo() != null){sb.append("\"jdNo\":\""+message.getJdNo()+"\",");}
    		if(message.getYhdNo() != null){sb.append("\"yhdNo\":\""+message.getYhdNo()+"\",");}
    		
    		sb.append("\"seqNo\":\""+ (++i) +"\"}");
    		
    	}
    	sb.append("]}");
        return sb.toString();
    }
    
    
    /**
     * @param security
     * @return
     */
    public static String getSecurityInfo(SecurityInfo security){
        StringBuffer sb = new StringBuffer("{\"signatureValue\":\"" + security.getSignatureValue()
                + "\",\"userName\":\""+security.getUserName()+"\",\"userPassword\":\"" + security.getUserPassword() + "\"}");
        return sb.toString();
    }
    
    
    //以下代码测试通过后清理
    public static String getMHeader()
    {
    	SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	String date = new String(fmt.format(new Date()));
        StringBuffer sb = new StringBuffer(
                "{\"orgCode\":\"3\",\"chnlId\":\"qhcs-dcs\",\"transNo\":\"Tran001\",\"transDate\":\""+date+"\",\"authCode\":\"CRT001A2\",\"authDate\":\""+date+"\"}");
        return sb.toString();
    }
    
    public static String getMHeader_DMZ(String orgCode, String chnlId, String transNo, Date transDate, String authCode, Date authDate){
    	SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	String aDate;
    	String date;
    	if(authDate == null){
    		date = new String(fmt.format(new Date()));
    	}else{
    		date = new String(fmt.format(authDate));
    	}
    	if(transDate == null){
    		aDate = new String(fmt.format(new Date()));
    	}else{
    		aDate = new String(fmt.format(transDate));
    	}
    	
        StringBuffer sb = new StringBuffer(
                "{\"orgCode\":\""+orgCode+"\","
                + "\"chnlId\":\""+ chnlId+"\","
                + "\"transNo\":\""+transNo+"\","
        		+ "\"transDate\":\""+aDate+"\","
				+ "\"authCode\":\""+authCode+"\","
				+ "\"authDate\":\""+date+"\"}");
        return sb.toString();
    }
    
    public static String getBusiData(String batchNo, String reasonCode, String idNo, String idType, String name, String seqNo)
    {
        StringBuffer sb = new StringBuffer(
                "{\"batchNo\":\""+batchNo+"\","
                + "\"records\":[{\"reasonNo\":\""+reasonCode+"\","
                + "\"idNo\":\""+idNo+"\","
                + "\"idType\":\""+idType+"\","
                + "\"mobileNo\":\""+"13761571319"+"\","
                + "\"name\":\""+name+"\","
                + "\"cardNo\":\""+"6225882119787585"+"\","
                + "\"seqNo\":\""+seqNo+"\"}]}");
        return sb.toString();
    }
    
    public static String getBusiData_MSC8004()
    {
    	//,\"entityAuthDate\":\""+date+"\",\"entityAuthCode\":\"CRT001A2\",\"cardNo\":\"6225882119787585\",\"mobileNo\":\"13761571319\",
    	SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	String date = new String(fmt.format(new Date()));
        StringBuffer sb = new StringBuffer(
                "{\"batchNo\":\"33ax3232sd1s\",\"records\":[{\"reasonCode\":\"01\",\"idNo\":\"370283198711174587\",\"idType\":\"0\",\"name\":\"宿彦慧\",\"seqNo\":\"r2315434334123\"}]}");
        return sb.toString();
    }

    public static String getSecurityInfo(String signatureValue, String pwd)
    {
        StringBuffer sb = new StringBuffer("{\"signatureValue\":\"" + signatureValue
                + "\",\"userName\":\"huoduiOper\",\"userPassword\":\"" + pwd + "\"}");
        return sb.toString();
    }
}
